package com.goldgov.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.URL;
import java.net.URLConnection;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class HttpUtil {
	public static void curl(String cmds[]){
		ProcessBuilder pb=new ProcessBuilder(cmds);
		pb.redirectErrorStream(true);
		Process p;
		try {
			p = pb.start();
			BufferedReader br=null;
			String line=null;
			
			br=new BufferedReader(new InputStreamReader(p.getInputStream()));
			while((line=br.readLine())!=null){
					System.out.println("\t"+line);
			}
			br.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void jsoup(String url){
		try {
			Document doc=Jsoup.connect(url)
			.timeout(10000)
			.ignoreContentType(true)
			.get();
			System.out.println(doc.html());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void url(String urlStr){
		try {
			String apiUrl = String.format(urlStr);
			URL url= new URL(apiUrl);
	        //开始请求
	        URLConnection open = url.openConnection();
	        InputStream input = open.getInputStream();
	        //这里转换为String，带上包名，怕你们引错包
	        String result = org.apache.commons.io.IOUtils.toString(input,"utf-8");
	        //输出
	        System.out.println(result);
		}catch(Exception e) {
			e.printStackTrace();
		}
    }
	
	public static void socket(String url) {
		try {
			// 想要抓取的网页主机名
			// 域名www.baidu.com,旗下有好多主机,例如tieba.baidu.com,map.baidu.com
			// 根据主机名获取ip地址
			InetAddress ip = InetAddress.getByName(url);
			// 建立连接
			Socket s = new Socket(ip, 80);
			// 向服务器端写入http协议请求
			PrintWriter pw = new PrintWriter(s.getOutputStream());
			// "/"请求根页面
			pw.println("GET / HTTP/1.1");
			pw.println("Host: " + url);
			pw.println("Content-Type: text/html");
			pw.println();
			pw.flush();
			// 将获取到的页面输出在控制台
			BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
			String str = "";
			while ((str = br.readLine()) != null) {
				System.out.println(str);
			}
			br.close();
			pw.close();
			s.close();
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	
//	public static void main(String[] args) {
//		String url="http://www.baidu.com";
//		
//		System.out.println("=====================jsoup");
//		HttpUtil.jsoup(url);
//		
//		System.out.println("======================curl");
//		String []cmds = {"curl", "-i", "-w", "状态%{http_code}；DNS时间%{time_namelookup}；"
//				+ "等待时间%{time_pretransfer}TCP 连接%{time_connect}；发出请求%{time_starttransfer}；"
//				+ "总时间%{time_total}",url};
//		HttpUtil.curl(cmds);
//		
//		System.out.println("========================url");
//		HttpUtil.url(url);
//		
//		System.out.println("=======================scoket");
//		HttpUtil.socket("www.baidu.com");
//	}
}